package plateforme.controlleur;

import java.sql.Connection;

import javax.servlet.http.HttpServlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

import plateforme.connexion.ConnectionManager;
import plateforme.dao.DaoAdmin;
import plateforme.model.Personne;


public class ControlleurPersonne extends HttpServlet  {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {
		
		
		String i=null;
		 String n=null;
		 String p = null;
		 String r=null;
		 String f=null;
		 String l=null;
		 String pa=null;
		 String em=null;
		
		
		
		
		try {
			
		
			
			
			Personne personne=new Personne();
			String login=request.getParameter("un");
			personne.setLogin(request.getParameter("un")); 
			personne.setPassword(request.getParameter("pw"));
			
			ResultSet rs;
			Connection cn=null;
			 cn=ConnectionManager.getConnection();
			// String sql = "select * from personne  where role='"+role+"'OR specialite='"+specialite+"'" ;
			String sql = "select * from personne  where login='"+login+"'" ;
			 Statement s = (Statement) cn.createStatement();

			  s.executeQuery (sql);

				
			  rs = (ResultSet) s.getResultSet();
			  

			  while (rs.next ()){
				  
				  
				
				  //Add records into data list

				  i=rs.getString(1);

				  n=rs.getString(2);

				  p=rs.getString(3);
				  
				  r=rs.getString(4);
				  f=rs.getString(5);
				  l= rs.getString(6);
				   pa=rs.getString(7);
				  em=rs.getString(8);
			  } 
			 
			 
			  
			personne = DaoAdmin.login(personne);
			
			
			if (personne.isValid()&& r.equals("admin")) {
				HttpSession session = request.getSession(true);
				session.setAttribute("currentSessionUser",personne);
				response.sendRedirect("admin.jsp"); 
				//logged-in page 
				} 
			
			
			
			
			else {
				
				if(personne.isValid()&& r.equals("chef"))
				{
					response.sendRedirect("chef_de_projet.jsp"); 
					 
				}
				else
				
				{ 
					
					 RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");  
			         dispatcher.forward( request, response ); 
				}
		}
		}
			catch (Throwable theException) 
				{ 
				System.out.println(theException);
				}
		
		
	}

}
